package conMgr.ui.util;

import java.awt.Component;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Insets;

import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;

public final class Ui
{
	/**
	 * The default insets to use for controls in this view (including
	 * subviews).
	 */
	public static final Insets DEFAULT_INSETS = new Insets(4, 4, 4, 4);
	
	/**
	 * Private constructor to prevent instantiation.
	 */
	private Ui() { }
	
	/**
	 * Creates a new button with a caption and action.
	 * @param caption The caption to display.
	 * @param action The action to take when this button is clicked.
	 * @return A new button.
	 */
	public static JButton createButton
	(
		final String caption,
		final Action action
	)
	{
		final JButton button = new JButton();
		button.setAction(action);
		button.setHideActionText(true);
		button.setText(caption);
		return button;
	}

	/**
	 * Gets the width of the widest data in the given column.
	 * @param table The table whose column is being measured.
	 * @param column The index of the column to measure.
	 * @param model The table column model.
	 * @return The width.
	 */
	private static int getDataWidth
	(
		final JTable table,
		final int column,
		final TableColumnModel model
	)
	{
		int width = 0;
		
		for (int r = 0; r < table.getRowCount(); r++)
		{
			final TableCellRenderer renderer = table.getCellRenderer(r, column);
		    final Component component =
	    		table.prepareRenderer(renderer, r, column);
		    width = Math.max(component.getPreferredSize().width, width);
		}
		
		return width;
	}
	
	/**
	 * Gets a bolded version of a font.
	 * @param original The font to bold.
	 * @return A bolded font.
	 */
	public static Font getUnboldFont(final Font original)
	{
		return new Font(original.getFamily(), Font.PLAIN, original.getSize());
	}
	
	/**
	 * Gets a value indicating if the given string is blank.
	 * @param text The string to test.
	 * True if the string is null or empty; otherwise, false. 
	 */
	public static boolean isBlank(final String text)
	{
		return text == null || text.equals("");
	}
	
	/**
	 * Packs a table to its preferred width.
	 * @param table The table to pack. 
	 */
	public static void packTable(final JTable table)
	{
		final JTableHeader header = table.getTableHeader();
		final FontMetrics metrics = header.getFontMetrics(header.getFont());
		final TableColumnModel model = table.getColumnModel();
		
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		
		for (int c = 0; c < table.getColumnCount(); c++)
		{
			int headerWidth = metrics.stringWidth(table.getColumnName(c));
			int dataWidth = getDataWidth(table, c, model);
			int width = Math.max(headerWidth, dataWidth);
			
			model.getColumn(c).setMinWidth(width + 10);
		}
	}
}
